﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AmbienteClient.Ambiente;
using System.Threading;
using Utilities;

namespace AmbienteClient
{
    public partial class Form1 : Form
    {
        private SoundControlSoapClient client = new SoundControlSoapClient();
        private GlobalKeyboardHook hook = new GlobalKeyboardHook();

        private bool button0check = false;
        private bool button1check = false;
        private bool button2check = false;
        private bool button3check = false;
        private bool button4check = false;
        private bool button5check = false;
        private bool button6check = false;
        private bool button7check = false;
        private bool button8check = false;
        private bool button9check = false;


        public Form1()
        {
            InitializeComponent();
            hook.HookedKeys.Add(Keys.NumPad0);
            hook.HookedKeys.Add(Keys.NumPad1);
            hook.HookedKeys.Add(Keys.NumPad2);
            hook.HookedKeys.Add(Keys.NumPad3);
            hook.HookedKeys.Add(Keys.NumPad4);
            hook.HookedKeys.Add(Keys.NumPad5);
            hook.HookedKeys.Add(Keys.NumPad6);
            hook.HookedKeys.Add(Keys.NumPad7);
            hook.HookedKeys.Add(Keys.NumPad8);
            hook.HookedKeys.Add(Keys.NumPad9);
            hook.KeyDown += new KeyEventHandler(hook_KeyDown);
        }

        void hook_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.NumPad0:
                    button0check = StartStop("BUTTON1", button0check);
                    break;
                case Keys.NumPad1:
                    button1check = StartStop("BUTTON2", button1check);
                    break;
                case Keys.NumPad2:
                    button2check = StartStop("BUTTON2", button2check);
                    break;
                case Keys.NumPad3:
                    button3check = StartStop("BUTTON3", button3check);
                    break;
                case Keys.NumPad4:
                    button4check = StartStop("BUTTON4", button4check);
                    break;
                case Keys.NumPad5:
                    button5check = StartStop("BUTTON5", button5check);
                    break;
                case Keys.NumPad6:
                    button6check = StartStop("BUTTON6", button6check);
                    break;
                case Keys.NumPad7:
                    button7check = StartStop("BUTTON7", button7check);
                    break;
                case Keys.NumPad8:
                    button8check = StartStop("BUTTON8", button8check);
                    break;
                case Keys.NumPad9:
                    button9check = StartStop("BUTTON9", button9check);
                    break;
                default:
                    break;
            }

        }

        private bool StartStop(String pButtonName, bool button)
        {
            bool temp;
            if (!button)
            {
                ThreadStart(pButtonName);
                temp = button = true;
            }
            else
            {
                ThreadStop(pButtonName);
                temp = button = false;
            }
            return temp;
        }

       

        private void ThreadStart(String button)
        {
            ParameterizedThreadStart pts = new ParameterizedThreadStart(this.play);
            Thread thread = new Thread(pts);
            thread.Start(button);
        }

        private void ThreadStop(String button)
        {
            ParameterizedThreadStart pts = new ParameterizedThreadStart(this.stop);
            Thread thread = new Thread(pts);
            thread.Start(button);
        }


        private void play(Object test)
        {
            try
            {
                client.PlaySound((String)test);
            }
            catch (Exception ex)
            {

            }
        }

        private void stop(Object button)
        {
            try
            {
                client.StopPlaySound((String)button);
            }
            catch (Exception ex)
            {

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1check = StartStop("BUTTON1", button9check);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button2check = StartStop("BUTTON2", button2check);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            button3check = StartStop("BUTTON3", button3check);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            button4check = StartStop("BUTTON4", button4check);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            button5check = StartStop("BUTTON5", button5check);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            button6check = StartStop("BUTTON6", button6check);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            button7check = StartStop("BUTTON7", button7check);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            button8check = StartStop("BUTTON8", button8check);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            button9check = StartStop("BUTTON9", button9check);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

       
    }


}
